package com.javaee.ch11.mapper;

import com.javaee.ch11.entity.BookCategory;
import com.javaee.ch11.web.dto.BookCategoryQueryDto;
import org.apache.ibatis.annotations.*;

import java.util.List;

// 图书分类Mapper接口，使用MyBatis注解实现数据库操作
@Mapper
public interface BookCategoryMapper {

    @Select("SELECT * FROM book_category")
    List<BookCategory> selectAll();

    // 根据ID查询图书分类
    @Select("SELECT * FROM book_category WHERE category_id = #{id}")
    BookCategory selectById(Long id);

    // 插入图书分类信息，不插入更新和创建时间，由数据库自动生成
    @Insert("INSERT INTO book_category (category_name, status,description) " +
            "VALUES (#{categoryName}, #{status}, #{description})")
    @Options(useGeneratedKeys = true, keyProperty = "categoryId")
    int insert(BookCategory bookCategory);

    // 根据ID更新图书分类信息
    @Update("UPDATE book_category " +
            "SET category_name = #{categoryName}, " +
            "status = #{status}, " +
            "description = #{description} " +
            "WHERE category_id = #{categoryId}")
    int updateById(BookCategory bookCategory);

    // 根据ID删除图书分类信息
    @Delete("DELETE FROM book_category WHERE category_id = #{id}")
    int deleteById(Long id);

    @Select("<script>" +
            "SELECT * FROM book_category " +
            "<where>" +
            "<if test='categoryId!= null'>" +
            " AND category_id = #{categoryId}" +
            "</if>" +
            "<if test='categoryName!= null and categoryName!= \"\"'>" +
            " AND category_name LIKE CONCAT('%', #{categoryName}, '%')" +
            "</if>" +
            "<if test='status!= null'>" +
            " AND status = #{status}" +
            "</if>" +
            "</where>" +
            " ORDER BY category_id DESC" +
            "</script>")
    List<BookCategory> queryBookCategories(BookCategoryQueryDto queryDto);
}